<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - PHP Application Library
 *
 * PHP version 5 (>= 5.2.10)
 *
 * Copyright (C) 2006-2011 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package    SyL.Lib
 * @subpackage SyL.Lib.Xml.Sitemap
 * @author     Koki Watanabe <k.watanabe@syl.jp>
 * @copyright 2006-2012 k.watanabe
 * @license    http://www.opensource.org/licenses/lgpl-license.php
 * @version    CVS: $Id:$
 * @link       http://syl.jp/
 * -----------------------------------------------------------------------------
 */

/** サイトマップURLクラス */
require_once 'SyL_XmlSitemapUrl.php';

/**
 * サイトマップ作成クラス
 *
 * @package    SyL.Lib
 * @subpackage SyL.Lib.Xml.Sitemap
 * @author     Koki Watanabe <k.watanabe@syl.jp>
 * @copyright 2006-2012 k.watanabe
 * @license    http://www.opensource.org/licenses/lgpl-license.php
 * @version    CVS: $Id:$
 * @link       http://syl.jp/
 */
class SyL_XmlSitemapWriter extends XMLWriter
{
    /**
     * URL配列
     * 
     * @var array
     */
    private $urls = array();

    /**
     * コンストラクタ
     *
     * @param string 出力ファイル
     */
    public function __construct($output_file=null)
    {
        if ($output_file) {
            $this->openURI($output_file);
        } else {
            $this->openMemory();
        }
        $this->setIndent(true);
        $this->setIndentString('  ');
    }

    /**
     * URLを追加する
     * 
     * @param SyL_XmlSitemapUrl サイトマップURLオブジェクト
     */
    public function addUrl(SyL_XmlSitemapUrl $sitemap)
    {
        $this->urls[] = $sitemap;
    }

    /**
     * Sitemapを作成する
     * 
     * @return string SitemapXML
     */
    public function createXml()
    {
        $this->startDocument('1.0','UTF-8');
        $this->startElement('urlset');
        $this->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
        $this->writeComment(' generated by SyL Framework Library ');

        foreach ($this->urls as &$sitemap) {
            $this->startElement('url');
            $this->writeElement('loc', $sitemap->loc);
            if ($sitemap->lastmod) {
                $this->writeElement('lastmod', $sitemap->lastmod->format('Y-m-d'));
            }
            if ($sitemap->changefreq) {
                $this->writeElement('changefreq', $sitemap->changefreq);
            }
            if ($sitemap->priority) {
                $this->writeElement('priority', $sitemap->priority);
            }
            $this->endElement();
        }
        $this->endElement();
        $this->endDocument();

        return $this->flush(true);
    }
}
